package transaction;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * transaction包,事务消息。half message 半消息
 *  todo 消费者端保持不变
 * @author itbac
 * @email 1218585258@qq.com
 * @date 2020/12/13
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者consumer，指定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group9");
        //2.指定nameserver地址
        consumer.setNamesrvAddr("192.168.0.88:9876");
        //3.订阅主题Topic 和 tag  （tag 可以用 || 表达式 ，也可以用 * ）
        // 订阅时，可以指定消息选择器,MessageSelector.bySql(),MessageSelector.byTag()
        consumer.subscribe("transaction", "*");
        //4.设置回调函数，处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                list.forEach(messageExt -> {
                    System.out.println("consumeMessage:" + new String(messageExt.getBody()));
                });
                //返回消费成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();



    }
}
